﻿import { segment } from "oicq";
import fetch from "node-fetch";
import fs from "fs";
import common from'../../lib/common/common.js'
const _path = process.cwd();


//
var dirpath = "resources/diqiu";//路径
let gailv = 0.4  //初始概率

export class example extends plugin {
  constructor() {
    super({
      /** 功能名称 */
      name: '透',
      /** 功能描述 */
      dsc: '透',
      /** https://oicqjs.github.io/oicq/#events */
      event: 'message',
      /** 优先级，数字越小等级越高 */
      priority: 40,
      rule: [
        {
          /** 命令正则匹配 */
          reg: "^#?透$",
          /** 执行方法 */
          fnc: 'c'
        },{
  /** 命令正则匹配 */
          reg: "^#?被透次数$",
          /** 执行方法 */
          fnc: 'cb'
}
      ]
    })
  }

  //执行方法



  async c(e) {
if (!fs.existsSync(dirpath)) {
			fs.mkdirSync(dirpath);
		  }
let name = e.message[1].text.replace("@", "")
var filename = "you.json";//文件名
var filename2 = `${name}.json`

 
let random_type = Math.random()
let at = e.at

let name2 = e.sender.nickname
   let url = `http://ovooa.com/API/QQ_level/?uin=${at}`
   let res = await fetch(url)
res = await res.json()
var filename = "you.json";
let url2 = `http://ovooa.com/API/yi/?QQ=${at}`
let url3 = `http://ovooa.com/API/yi/?QQ=${e.user_id}`
let image = segment.image(url2)
let image2 = segment.image(url3)
let cao = `你与${name}发起pk，你长枪一挺，精准一枪洞穿${name}。。。${name}飞出几十米远撞在远处的崖壁上，晕了1分钟在晕的60秒内整整被你透了60次，${name}卒。`
let cao2 = `你刚想透${name}的时候，被${name}猛然一个转身擒拿住了，对你使用疯狂乱透将你反杀，皮燕子都被${name}橄榄了。之后，${name}提上枪潇洒离去，你躺在地上足足有1分钟动弹不得，${name2}卒。`
 if (!fs.existsSync(dirpath+"/"+filename)) {
fs.appendFileSync(dirpath + "/" + filename, JSON.stringify({
		  "透":
		  {
			"名字": name,
"QQ号": at,
		  },
		}))
}
if (fs.existsSync(dirpath+"/"+filename)) {
fs.appendFileSync(dirpath + "/" + filename, JSON.stringify({
		  "透":
		  {
			"名字": name,
"QQ号": at,
		  },
		}))
var ress = fs.readFileSync(dirpath + "/" + filename,"utf8")
            let index = ress.indexOf(`${name}`)
            let num = 0
            while (index !== -1) {
                num++
                
                index = ress.indexOf(`${name}`, index + 1)
       }
if (!fs.existsSync(dirpath + "/" + filename2)) {
fs.writeFileSync(dirpath + "/" + filename2, JSON.stringify({
		  "透":
		  {
			"名字": name,
"次数": num,
		  },
		}))

}
if (fs.existsSync(dirpath + "/" + filename2)) {
fs.writeFileSync(dirpath + "/" + filename2, JSON.stringify({
		  "透":
		  {
			"名字": name,
"次数": num,
		  },
		}))

}
 if(random_type < gailv){
e.reply(cao2)
await common.sleep(3000)
let msg2 = [image2,`我还没能,没能.......啊！ `]
e.reply(msg2)
}else{
e.reply(cao)
await common.sleep(3000)
let msg3 = [image,`......细🐶，你，你就是不行！`]
e.reply(msg3)
}
}
}
async cb(e) {
if (e.msg.includes('被透次数')){
let name = e.message[1].text.replace("@", "")
var str = name
var filename = "you.json";//文件名
 var ress = fs.readFileSync(dirpath + "/" + filename,"utf8")
            let index = ress.indexOf(`${name}`)
            let num = 0
            while (index !== -1) {
                num++
                
                index = ress.indexOf(`${name}`, index + 1)
            }
	  console.log('一共' + num);
let at = e.at
let url = `http://ovooa.com/API/QQ_level/?uin=${at}`
   let res = await fetch(url)
res = await res.json()
let head = await res.data.head
let word_list=[
'唔姆~',
'嗯，不多不多',
'感觉还没透够呢～',
'要再透一次吗？'];//这边文案自己改
let text = Math.ceil(Math.random() * word_list['length'])


let msg = [segment.image(head),`这个家伙已经被透了${num}次`,"\n",`${word_list[text-1]}`]
e.reply(msg)
}
}

}

















